package com.marvels.financial.utils;

import java.math.BigInteger;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Random;

public class ReqGenerateUtil {
	
	/**
     * 生成交易流水号
     * @return
     */
    public static String genTransctionSeq(String prefix) {
        StringBuffer sb = new StringBuffer();
        GregorianCalendar gc = new GregorianCalendar();
        Date date = new Date();
        gc.setTime(date);
        Random r = new Random();
        sb.append(prefix).append(gc.getTimeInMillis())
                .append(formatStr(String.valueOf(new BigInteger(165, r).toString(10).toUpperCase().substring(0, 8)),
                        8, true, "0"));
        return sb.toString();
    }
    
    
    /**
     * 格式化字符串
     * @param str
     * @param length
     * @param isBefore
     * @param str1
     * @return
     */
    public static String formatStr(String str, int length, boolean isBefore,
                                   String str1) {
        int l = length - str.length();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < l; i++) {
            sb.append(str1);
        }
        if (isBefore == true) {
            return sb.toString() + str;
        } else {
            return str + sb.toString();
        }
    }
}
